package ps.client.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;


@SuppressWarnings("serial")
public class SendMessagePanel extends JPanel {
    
    JTextField messageField = new JTextField();
    JButton sendButton = new JButton("Senden");
    
    String messageToSend = "";
    
    public SendMessagePanel() {
        setBorder(new LineBorder(Color.LIGHT_GRAY, 3));
        setBackground(Color.LIGHT_GRAY);
        setLayout(new BorderLayout(3, 3));
        
        add(messageField, BorderLayout.CENTER);
        add(sendButton, BorderLayout.EAST);
        
//        messageField.setForeground(Color.WHITE);
//        messageField.setBackground(Color.GRAY);
//        messageField.setCaretColor(Color.WHITE);
        messageField.setBorder(new EmptyBorder(2, 4, 2, 4));
        
        sendButton.setBackground(Color.LIGHT_GRAY);
        
        sendButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sendMessage();
            }
        });
        messageField.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                sendButton.doClick();
            }
        });
    }
    
    private void sendMessage() {
        messageToSend = messageField.getText().trim();
        if (messageToSend.length() > MainFrame.MAX_STRING_LENGTH) {
            JOptionPane.showMessageDialog(this, "Die Nachricht ist zu lang.", "Nachricht senden", JOptionPane.ERROR_MESSAGE);
        } else {
    //        messageField.setText("########################################################################################################################");
            messageField.setText("");
            if (messageToSend.length() > 0) {
                fireSendMessage(messageToSend);
            }
        }
        messageField.requestFocus();
    }
    
    public String getMessageToSend() {
        return messageToSend;
    }
    
    protected void fireSendMessage(String msg) {
    }

}
